package com.quanyan.stadium.service.gameRules.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.TbGameEvent;
import com.quanyan.stadium.entity.TbGameRound;
import com.quanyan.stadium.entity.TbGameSchedule;
import com.quanyan.stadium.service.gameRules.AbsGameSystem;
import com.quanyan.stadium.service.gameRules.entity.GameRuleGenerate;
import com.quanyan.stadium.utils.GameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxiao
 * @date 16/4/11-14:42
 *
 */

@Service("gameRuleWashedServiceImpl")
public class GameRuleWashedServiceImpl extends AbsGameSystem {

    private final static Logger logger = LoggerFactory.getLogger(GameRuleWashedServiceImpl.class);

    @Autowired
    private GameCategoryServiceImpl gameCategoryService;

    @Autowired
    private BizGameSystemDao bizGameSystemDao;


    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class, timeout = 10)
    public List<Integer> createGameRound(Integer gameEventId, Integer currentTeamNum, Map<String, Object> params) throws Exception {
        TbGameEvent tbGameEvent = bizGameSystemDao.selectTbGameEvent(gameEventId);
        currentTeamNum = gameCategoryService.getTeamNum(tbGameEvent, currentTeamNum);
        int roundNum = GameUtils.washedRoundleNum(currentTeamNum.doubleValue());
        int rankWash = (int)Math.pow(2, GameUtils.washedRoundleNum((double) currentTeamNum))/2;
        List<Integer> result = new ArrayList<>();
        for (int i=0; i < roundNum; i++) {
            TbGameRound gameRound = new TbGameRound();
            if (i==0) {
                gameRound.setCurrentTeamNum(currentTeamNum);
                gameRound.setRankTeamNum(rankWash);
            }else{
                gameRound.setCurrentTeamNum(rankWash);
                gameRound.setRankTeamNum(rankWash/2);
                rankWash = rankWash/2;
            }
            gameRound.setRoundStatus((byte) (i+1));
            gameRound.setGameEventId(tbGameEvent.getId());
            gameRound.setPlaceId(tbGameEvent.getPlaceId());
            if (null == bizGameSystemDao.insertTbGameRound(gameRound).getId()) {
                throw new Exception("数据库异常");
            }
            result.add(gameRound.getId());
        }

        //更新赛事进行中的轮次
        TbGameEvent event = new TbGameEvent();
        event.setId(gameEventId);
        event.setCurrentRoundId(result.get(0));
        bizGameSystemDao.updateTbGameEvent(event);

        //生成所有赛程
        initAllGameSchedule(result);

        //用户第一轮的队伍信息


        return result;
    }

    /**
     * 根据轮次来按轮次来创建赛程数目,循环轮次生成对应的赛程
     *
     * */
    public void initAllGameSchedule(List<Integer> gameRoundIds){
        if (CollectionUtils.isNotEmpty(gameRoundIds)) {
            int roundNum = gameRoundIds.size();
            for (int i=0;i<roundNum;i++){
                int temp = (int) Math.pow(2.0, roundNum-1-i);
                for (int j=0;j<temp;j++) {
                    TbGameSchedule tbGameSchedule = new TbGameSchedule();
                    tbGameSchedule.setGameRoundId(gameRoundIds.get(i));
                    bizGameSystemDao.insertTbGameSchedule(tbGameSchedule);
                }
            }
        }
    }


    /**
     *  更新该轮的参赛用户
     *
     * */
    public void initFirstGameSchuele(Integer gameRoundId, List<Integer> teamIds){
        List<TbGameSchedule> schedules = bizGameSystemDao.selectTbGameScheduleByGameRoundId(gameRoundId, 0, GameConstants.GAME_SCHEDULE_NUM);
        Collections.shuffle(teamIds);    //淘汰赛洗牌模式
        //List<GameGroup> gameGroups = gameSystemService.washedGroup(this.transferTeamIdToDoubleTeam(reqChallenge.getTbTeamDoubleAddendumList(), new ArrayList(teamIs)), tbGameRound, true);

    }

    @Override
    public List<Integer> createGameSchedule(TbGameEvent tbGameEvent, GameRuleGenerate gameRuleGenerate) {
        return null;
    }
}
